﻿using CodeGenerator.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGenerator.Template.Web.Views
{
    public partial class Index : ITemplate
    {
        public MetaData Data { get; set; }
        public List<LookupClassModel> lookups { get; set; }
        public List<PickupListModel> pickuplist { get; set; }

        public string Filename
        {
            get { return "Index.cshtml"; }
        }
        public string FolderName
        {
            get { return "{ClassName}GridEdit"; }
        }

        public bool IsGenerateOneTime { get { return false; } }
        public bool IsGenerateFolder { get { return true; } }
        public bool IsRequiredOnlyOneMember { get { return true; } }
        public GenerateStyle GenerateStyle { get { return GenerateStyle.Normal; } }

        public string GenCalculationFieldItem(AttributeModel attr)
        {
            string Result = "";
            string TempFormula = attr.Formula;
            foreach (var item in Data.Models[0].GetAttributes())
            {
                if (item.DataType.Name.ToString() == "int" || item.DataType.Name.ToString() == "long" || item.DataType.Name.ToString() == "decimal" || item.DataType.Name.ToString() == "double" || item.DataType.Name.ToString() == "float")
                {
                    TempFormula = TempFormula.Replace("$" + Data.Models[0].Id + "." + item.Id, "parseFloat(this." + item.Name + "()) ");
                }
                else
                {
                    TempFormula = TempFormula.Replace("$" + Data.Models[0].Id + "." + item.Id, "this." + item.Name + "() ");
                }
            }
            //string Format = "this." + attr.Name + " = ko.dependentObservable(function () { return ({0}); },this);";

            if (attr.DataType.Name.ToString() == "DateTime")
            {
                Result = "this." + attr.Name + " = ko.dependentObservable(function () { var result = ( " + TempFormula + "); return result; },this);";
            }
            else
            {
                Result = "this." + attr.Name + " = ko.dependentObservable(function () { var result = ( " + TempFormula + "); if(isNaN(result)) return ''; return result; },this);";
            }
            return Result;
        }
        public string GenCalculationFieldItemChild(ClassModel child,AttributeModel attr)
        {
            string Result = "";
            string TempFormula = attr.Formula;
            foreach (var item in child.GetAttributes())
            {
                if (item.DataType.Name.ToString() == "int" || item.DataType.Name.ToString() == "long" || item.DataType.Name.ToString() == "decimal" || item.DataType.Name.ToString() == "double" || item.DataType.Name.ToString() == "float")
                {
                    TempFormula = TempFormula.Replace("$" + child.Id + "." + item.Id, "parseFloat(this." + item.Name + "()) ");
                }
                else
                {
                    TempFormula = TempFormula.Replace("$" + child.Id + "." + item.Id, "this." + item.Name + "() ");
                }
            }
            //string Format = "this." + attr.Name + " = ko.dependentObservable(function () { return ({0}); },this);";

            if (attr.DataType.Name.ToString() == "DateTime")
            {
                Result = "this." + attr.Name + " = ko.dependentObservable(function () { var result = ( " + TempFormula + "); return result; },this);";
            }
            else
            {
                Result = "this." + attr.Name + " = ko.dependentObservable(function () { var result = ( " + TempFormula + "); if(isNaN(result)) return ''; return result; },this);";
            }
            return Result;
        }
        public string GenSummaryFieldItem(AttributeModel attr)
        {
            string TempFormula = attr.Formula;
            string[] temp = TempFormula.Split('.');
            int ClassId = Convert.ToInt32(temp[0]);
            int AttrId = Convert.ToInt32(temp[1]);
            var ClassModel = Data.Models[0].ChildrenClass.Where(t => t.Id == ClassId).First();
            var AttributeTemp = ClassModel.GetAttributes().AsEnumerable().Where(x => x.Id.Equals(AttrId)).First();
            string Result = @"this." + attr.Name + @" = ko.dependentObservable(function () {
                var total = 0;
                ko.utils.arrayForEach(this." + ClassModel.Name + @"(), function (data) {
                    total += parseFloat(data." + AttributeTemp.Name + @"());
                });
                if(isNaN(total)) return '';
                return total;
            }, this);";
            return Result;
        }
        public string GenFooterSummaryFieldItem(AttributeModel attr)
        {
            string Result = "";
            string TempFormula = attr.Formula;
            string[] temp = TempFormula.Split('.');
            int ClassId = Convert.ToInt32(temp[0]);
            int AttrId = Convert.ToInt32(temp[1]);
            var ClassModel = Data.Models[0].ChildrenClass.Where(t => t.Id == ClassId).First();
            var AttrDisplay = ClassModel.GetAllDisplay().GetAttributes().OrderBy(item => item.SeqNo).ToList();
            Result += "<td>Total : </td>";
            foreach (var item in AttrDisplay)
            {
                if (item.DisplayLengthInGrid > 0)
                {
                    string display = "";
                    if (item.Autokey)
                    {
                        display = "style=\"display:none;\"";
                    }
                    if (AttrId == item.Id)
                        Result += string.Format("<td {0}><label data-bind=\"text: " + attr.Name + "\" /></td>", display);
                    else
                        Result += string.Format("<td {0}></td>", display);
                }
            }
            return "<tr>" + Result + "<td></td></tr>";
        }
        //public string GenCalculationFiled(AttributeModel attr)
        //{
        //    attr.ClassModel.Id
        //}

    }
}
